package second.progr.helpstaff;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JTextFieldOperator;
import org.netbeans.jemmy.operators.JTreeOperator;
import org.testng.internal.annotations.DefaultAnnotationTransformer;


public class FolderHelper extends HelperBase{

	public FolderHelper(ApplicationManager applicationManager) {
		super(applicationManager);	
	}

	public FoldersList getFolders() {
		List<String> list = new ArrayList<String>();
		JTreeOperator tree = new JTreeOperator(mainFrame);
		Object[] children = tree.getChildren(tree.getRoot());	
		for (Object child : children) {
			list.add("" + child);
		}
		return new FoldersList(list);
	}

	public String clickOnFolderAndDelete() {
		JTreeOperator tree = new JTreeOperator(mainFrame);
		Object[] children = tree.getChildren(tree.getRoot());
		Random rnd = new Random();
	    int folderIndex = rnd.nextInt(children.length);
		tree.selectRow(folderIndex-1);
		String childName = children[folderIndex-1].toString();	
		manager.getMenuHelper().pushDeleteFolder();
	    JDialogOperator dialog = new JDialogOperator(mainFrame);
	    new JButtonOperator(dialog, "Yes").push();
	    if (childName.isEmpty()){
	      return waitMessageDialog("Warning", 3000);
	    }
	    else{
	      return childName;
	    }
	}
	
	public String createFolder(String folderName) {	
	    manager.getMenuHelper().pushCreateFolder();
	    JDialogOperator dialog = new JDialogOperator(mainFrame);
	    new JTextFieldOperator(dialog).setText(folderName);
	    new JButtonOperator(dialog, "OK").push();
	    return waitMessageDialog("Warning", 3000);
	}
}